package com.rongshu.view.home.fragment.dialog;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.SpannableStringUtils;
import com.rongshu.view.home.listener.OnDisPlayModeClickListener;
import com.utils.SPUtils;
import com.utils.constants.SPKey;
import com.widget.SwitchButton;

public class DisplayModeDialog extends Dialog {

    public DisplayModeDialog(@NonNull Context context) {
        super(context);
    }

    public DisplayModeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected DisplayModeDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {

        private Context context = null;

        private int mMainLayout = R.layout.fragment_display_mode_dialog_view;

        private OnDisPlayModeClickListener mOnDisPlayModeClickListener = null;

        private SwitchButton switch_read_mode;

        private TextView txt_large, txt_middle, txt_small;
        private RelativeLayout layout_close;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setOnDisPlayModeClickListener(OnDisPlayModeClickListener listener) {
            this.mOnDisPlayModeClickListener = listener;
            return this;
        }

        public DisplayModeDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final DisplayModeDialog dialog = new DisplayModeDialog(context, R.style.CustomDialogStyle);
            View layout = inflater.inflate(mMainLayout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);
            Window window = dialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.setWindowAnimations(R.style.DefaultAnimation);

            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);

            switch_read_mode = layout.findViewById(R.id.switch_read_mode);
            txt_large = layout.findViewById(R.id.txt_large);
            txt_middle = layout.findViewById(R.id.txt_middle);
            txt_small = layout.findViewById(R.id.txt_small);
            layout_close = layout.findViewById(R.id.layout_close);
            //text
            final String large = txt_large.getText().toString().trim();
            final String middle = txt_middle.getText().toString().trim();
            final String small = txt_small.getText().toString().trim();
            //设置是否保存过夜间
            boolean isOff = SPUtils.getPreferences(SPKey.SKIN_STATE, false);
            switch_read_mode.setChecked(isOff);
            //设置是否保存过字体类型
            final int fontType = SPUtils.getPreferences(SPKey.FONTPATTERN, 0);
            updateText(context, txt_small, fontType == 0 ? true : false, small);
            updateText(context, txt_middle, fontType == 1 ? true : false, middle);
            updateText(context, txt_large, fontType == 2 ? true : false, large);

            switch_read_mode.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                    if (mOnDisPlayModeClickListener != null) {
                        mOnDisPlayModeClickListener.onNightMode(isChecked);
                    }
                }
            });
            txt_large.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateText(context, txt_small, false, small);
                    updateText(context, txt_middle, false, middle);
                    updateText(context, txt_large, true, large);
                    if (mOnDisPlayModeClickListener != null) {
                        mOnDisPlayModeClickListener.onFontTypes(2);
                    }
                }
            });

            txt_middle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateText(context, txt_small, false, small);
                    updateText(context, txt_middle, true, middle);
                    updateText(context, txt_large, false, large);
                    if (mOnDisPlayModeClickListener != null) {
                        mOnDisPlayModeClickListener.onFontTypes(1);
                    }
                }
            });

            txt_small.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateText(context, txt_small, true, small);
                    updateText(context, txt_middle, false, middle);
                    updateText(context, txt_large, false, large);
                    if (mOnDisPlayModeClickListener != null) {
                        mOnDisPlayModeClickListener.onFontTypes(0);
                    }
                }
            });

            layout_close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            return dialog;
        }
    }

    /**
     * 字体颜色，下滑线
     */
    private static void updateText(Context context, TextView view, boolean isCheck, String content) {
        view.setTextColor(context.getResources().getColor(isCheck ? R.color.color_3cc051 : R.color.color_9f9f9f));
        view.setText(isCheck ? SpannableStringUtils.addUnderLineSpan(content, 0, content.length()) : content);
    }
}
